package com.nuaa.service.transfer.impl;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nuaa.dao.transfer.inter.SupDaoInter;
import com.nuaa.dao.transfer.inter.TransferDaoInter;
import com.nuaa.domain.change.ChangeEntity;
import com.nuaa.domain.transfer.TransSupEntity;
import com.nuaa.domain.transfer.TransferEntity;
import com.nuaa.formmodel.ChanHandForm;
import com.nuaa.formmodel.TransHandForm;
import com.nuaa.service.transfer.inter.TransferServiceInter;
import com.nuaa.utils.AjaxMsg;
import com.nuaa.utils.BusinessException;
import com.nuaa.utils.DataGrid;
import com.nuaa.utils.PageInfo;
@Service("transferService")
public class TransferServiceImpl implements TransferServiceInter{
	@Resource(name="transferDao")
	private TransferDaoInter transferDao;
	@Resource(name="transSupDao")
	private SupDaoInter transSupDao;
	/**保存一条中转订单信息*/
	@Override
	public AjaxMsg saveTansferOrder(TransferEntity entity) {
		return transferDao.saveTransferOrder(entity);
	}
	/**获取所有订单信息*/
	@Override
	public DataGrid<Map<String, Object>> getTransferOrders(PageInfo pager,String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from transfer_order where 1=1 ");

		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=transferDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=transferDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	/**获取所有订单信息*/
	@Override
	public DataGrid<Map<String, Object>> getWaitTransferOrders(PageInfo pager,String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from transfer_order where state=0 ");

		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=transferDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=transferDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	
	@Override
	public DataGrid<Map<String, Object>> getProceTransferOrders(PageInfo pager, String customer_no, String orderNo,
			String tmFrom, String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from transfer_order where state>0 and state<6 ");

		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=transferDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=transferDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	public DataGrid<Map<String, Object>> getFinishTransferOrders(PageInfo pager,String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from transfer_order where state=6 ");

		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=transferDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=transferDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	public TransferEntity getById(int id) {
		return transferDao.find(TransferEntity.class, id);
	}
	
	@Transactional
	@Override
	public void update(TransferEntity transfer) {
		transferDao.update(transfer);
		
	}
	@Transactional
	@Override
	public AjaxMsg saveModOrder(TransferEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		TransferEntity transEntity = transferDao.find(TransferEntity.class, entity.getId());
		transEntity.setBox_total(entity.getBox_total());
		transEntity.setLength(entity.getLength());
		transEntity.setWidth(entity.getWidth());
		transEntity.setHeight(entity.getHeight());
		transEntity.setAve_weight(entity.getAve_weight());
		transEntity.setIfRePack(entity.getIfRePack());
		try{
			transferDao.save(transEntity);
			msg.setSuccess(true);
			msg.setMsg("修改成功！");
		}catch (Exception e) {
			msg.setSuccess(false);
			msg.setMsg("修改失败！");
		}
		return msg;
	}
	@Override
	public DataGrid<Map<String, Object>> getTransferUncheck(PageInfo pager) {

		StringBuilder sql=new StringBuilder();
		sql.append("select * from transfer_order where 1=1 and state = 0 ");
		try{
			long total=transferDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=transferDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Transactional
	@Override
	public AjaxMsg subSup(TransSupEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		try{
			//首先判断是否提交过该订单的补充文档
			StringBuilder sql=new StringBuilder();
			sql.append("select * from transfer_sup where order_no = '");
			sql.append(entity.getOrder_no());
			sql.append("'");
			List<TransSupEntity> list = transSupDao.search(TransSupEntity.class, sql.toString());
			if(list==null || list.size()==0){
				transSupDao.save(entity);
			}else{
				/***
				 * 此处既然是覆盖就该删掉原有的文档，留做以后修正吧
				 */
				TransSupEntity temp = list.get(0);
				if(!entity.getDeliver_number_tb().equals("")){
					this.delFileByName(temp.getDeliver_number_tb());
					temp.setDeliver_number_tb(entity.getDeliver_number_tb());
				}
					
				if(!entity.getDeliver_order_tb().equals("")){
					this.delFileByName(temp.getDeliver_order_tb());
					temp.setDeliver_order_tb(entity.getDeliver_order_tb());
				}
				
				if(!entity.getOther_file().equals("")){
					this.delFileByName(entity.getOther_file());
					temp.setOther_file(entity.getOther_file());
				}
				if(!entity.getOther_file2().equals("")){
					this.delFileByName(entity.getOther_file2());
					temp.setOther_file2(entity.getOther_file2());
				}
				if(!entity.getOther_file3().equals("")){
					this.delFileByName(entity.getOther_file3());
					temp.setOther_file3(entity.getOther_file3());
				}	
				if(!entity.getOther_file4().equals("")){
					this.delFileByName(entity.getOther_file4());
					temp.setOther_file4(entity.getOther_file4());
				}
				transSupDao.update(temp);
			}
			msg.setSuccess(true);
			msg.setMsg("提交成功！");
		}catch(Exception e){
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("提交失败！");
		}
		return msg;
	}
	@Override
	public TransferEntity getByOrderNo(String order_no) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from transfer_order where order_no = '");
		sql.append(order_no);
		sql.append("'");
		List<TransferEntity> list = transferDao.search(TransferEntity.class, sql.toString());
		if(list.size()==0)
			return null;
		return list.get(0);
	}
	@Override
	public TransSupEntity getSupByOrderNo(String order_no) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from transfer_sup where order_no = '");
		sql.append(order_no);
		sql.append("'");
		List<TransSupEntity> list = transSupDao.search(TransSupEntity.class, sql.toString());
		if(list.size()==0)
			return null;
		return list.get(0);
	}
	@Override
	public void delFileByName(String fileName) {
		String dir = System.getProperty("webApp.path")+"file";
		if(!"".equals(fileName)){
			File file = new File(dir+"\\"+fileName);
			if(file.exists()){
				file.delete();
			}
		}
	}
	
	@Transactional
	@Override
	public AjaxMsg saveHand(String id, TransHandForm form) {
		AjaxMsg msg = new AjaxMsg();
		try{
				TransferEntity entity = transferDao.find(TransferEntity.class,Integer.parseInt(id));
				entity.setState(form.getState());
				entity.setLaborFee(form.getLaborFee());
				entity.setPostage(form.getPostage());
				
				if(form.getArr_tab_name()!=null&&!form.getArr_tab_name().equals("")){
					this.delFileByName(entity.getArr_tab_name());
					entity.setArr_tab_name(form.getArr_tab_name());
				}
				if(form.getArr_check_result_name()!=null&&!form.getArr_check_result_name().equals("")){
					this.delFileByName(entity.getArr_check_result_name());
					entity.setArr_check_result_name(form.getArr_check_result_name());
				}
				if(form.getArr_tab_name()!=null&&!form.getArr_tab_name().equals("")){
					this.delFileByName(entity.getArr_tab_name());
					entity.setArr_tab_name(form.getArr_tab_name());
				}
				if(form.getRec_tab_name()!=null&&!form.getRec_tab_name().equals("")){
					this.delFileByName(entity.getRec_tab_name());
					entity.setRec_tab_name(form.getRec_tab_name());
				}
				if(form.getTran_deliver_tab_name()!=null&&!form.getTran_deliver_tab_name().equals("")){
					this.delFileByName(entity.getTran_deliver_tab_name());
					entity.setTran_deliver_tab_name(form.getTran_deliver_tab_name());
				}
				if(form.getTrans_model_name()!=null&&!form.getTrans_model_name().equals("")){
					this.delFileByName(entity.getTrans_model_name());
					entity.setTrans_model_name(form.getTrans_model_name());
				}
				
				if(form.getOther_file1()!=null&&!form.getOther_file1().equals("")){
					this.delFileByName(entity.getOther_file1());
					entity.setOther_file1(form.getOther_file1());
				}
				if(form.getOther_file2()!=null&&!form.getOther_file2().equals("")){
					this.delFileByName(entity.getOther_file2());
					entity.setOther_file2(form.getOther_file2());
				}
				if(form.getOther_file3()!=null&&!form.getOther_file3().equals("")){
					this.delFileByName(entity.getOther_file3());
					entity.setOther_file3(form.getOther_file3());
				}
				if(form.getOther_file4()!=null&&!form.getOther_file4().equals("")){
					this.delFileByName(entity.getOther_file4());
					entity.setOther_file4(form.getOther_file4());
				}
				transferDao.update(entity);
			msg.setSuccess(true);
			msg.setMsg("操作成功！");
		}catch (Exception e) {
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("操作失败！");
		}
		return msg;
	}
}
